package jdbc;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.JdbcUtil2;

import java.sql.Connection;
import java.sql.SQLException;

public class UtilsSelectScalarHandler {
    public static void main(String[] args) throws SQLException {
        //创建dbUtils里面的QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();
        //sql语句
        String sql = "select max(age) from stu";
        //获取数据库连接
        Connection connection = JdbcUtil2.getConnection();
        //存参数值的数组
        Object[] params = {};
        //执行查询，并以数组的形式返回查询结果（new ScalarHandler<>()返回分组函数的值）
        int age = queryRunner.query(connection,sql, new ScalarHandler<>(),params);
        System.out.println(age);
        //关闭数据库连接
        connection.close();
    }
}
